********************************************************************************
*                                                                              *
*             FIRM-LEVEL ANALYSES OF THE IMPACT OF ROBOT COMPETITION           *
*                                                                              *
********************************************************************************

cd "H:\"

// Variable list for fixed effects regressions
global feweightvar hoursworked09
global fecvar beid sbi2#year mun#year c.logvalueaddedpworker09#year c.logworkers09#year
global cluster beidXyear

// What do we do below:
*1) Robot competition, instrumented (robot exposure, t-5)
*2) First-stage results
*3) OLS panel competition regressions 
*4) Robot competition, sensitivity of the instrument


		
////////////////////////////////////////////////////////////////////////////////
/////////1) COMPETITION PANEL DATA ANALYSIS WITH EXPOSURE INSTRUMENT////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B12

use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0
	merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	egen beidXyear = group(beid year)
	egen industry_ifrXyear = group(industry_ifr year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)

// Regressions
ivreghdfe logvalueadded (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted1
	scalar γ1 = _b[compete]
ivreghdfe laborshare (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted3
	scalar γ3 = _b[compete]
ivreghdfe loghwage (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted4
	scalar γ4 = _b[compete]
ivreghdfe loghoursworked (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted5
	scalar γ5= _b[compete]
	
qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_baseline", label aster replace word tex nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_baseline", label aster append word tex nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_baseline", label aster append word tex nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_baseline", label aster append word tex nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) ctitle(Hours worked \textit{(log)}) nocon
				
	
// Predict total effects 
// !!Run "Firm-level regressions - 02) Panel data regressions.do" first to obtain β's!!

preserve 
	use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	g total_valueadded = ((exp(γ1)-1)*compete + (exp(β1)-1)*robots)*valueadded	
	g hourly_wages = ((exp(γ4)-1)*compete + (exp(β4)-1)*robots)*empl*hwage
	g total_hoursworked = ((exp(γ5)-1)*compete + (exp(β5)-1)*robots)*hoursworked

	keep if year == 2020
	collapse (sum) total_valueadded hourly_wages total_hoursworked valueadded hoursworked empl (count) beid, by(sbi4)
	rename (sbi4 beid) (Industry N)
	tostring Industry, force replace
	
	replace hourly_wages = hourly_wages/empl
	local M = _N+1
	set obs `M'
	
	total total_valueadded hourly_wages total_hoursworked empl valueadded hoursworked N
	replace Industry = "TOTAL" in `M'
	foreach value in total_valueadded hourly_wages total_hoursworked empl valueadded hoursworked N {
		replace `value' = _b[`value'] in `M'
	}
		
	rename (total_valueadded hourly_wages total_hoursworked valueadded hoursworked empl) (ΔGVA ΔHWages ΔHoursw GVA Hoursw Empl)
	drop if N < 10
	export excel using "Robots and Workers\Results\Tables.xlsx", sheet("Competition") sheetmodify firstrow(variables)
	
restore
	
////////////////////////////////////////////////////////////////////////////////
//////////2) COMPETITION PANEL DATA ANALYSIS  - FIRST-STAGE RESULTS/////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B13

use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0
	merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	egen beidXyear = group(beid year)
	egen industry_ifrXyear = group(industry_ifr year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)
	
reghdfe compete robotsexposure_tmin5 robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
estimates store fsweighted1

qui estimates restore fsweighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_FS", label aster replace word tex ctitle(Competition, weighted) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)	
	
	
	

////////////////////////////////////////////////////////////////////////////////
///////////////////////3) COMPETITION PANEL DATA ANALYSIS - OLS/////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B14

use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0

	egen beidXyear = group(beid year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)

reghdfe logvalueadded compete robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
reghdfe laborshare compete robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted3
reghdfe loghwage compete robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked compete robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted5
	
qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_OLS_final", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_OLS_final", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_OLS_final", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_Competition_OLS_final", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
	
	
	
	
////////////////////////////////////////////////////////////////////////////////
///////////////////4) COMPETITION INSTRUMENT -- SENSITIVITY/////////////////////
////////////////////////////////////////////////////////////////////////////////

//////////////////////// a)  t-6 to avoid extrapolation ////////////////////////

* Create Table B15, Panel A

use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0
	merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare_tmin6.dta", nogen keep(1 3)

	egen beidXyear = group(beid year)
	egen industry_ifrXyear = group(industry_ifr year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)
	keep if year > 2009

ivreghdfe logvalueadded (compete=robotsexposure_tmin6) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted1
ivreghdfe laborshare (compete=robotsexposure_tmin6) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted2
ivreghdfe loghwage (compete=robotsexposure_tmin6) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted3
ivreghdfe loghoursworked (compete=robotsexposure_tmin6) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted4

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin6", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.) addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted2
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin6", label aster append word tex ctitle(GVA per hour \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin6", label aster append word tex ctitle(Labor share) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted4
    outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin6", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2


///////////////////// b)  current year with extrapolation //////////////////////

* Create Table B15, Panel B
	
use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0
	merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	egen beidXyear = group(beid year)
	egen industry_ifrXyear = group(industry_ifr year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)

ivreghdfe logvalueadded (compete=robotsexposure) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted1
ivreghdfe laborshare (compete=robotsexposure) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted2
ivreghdfe loghwage (compete=robotsexposure) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted3
ivreghdfe loghoursworked (compete=robotsexposure) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted4

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin0", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.) addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted2
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin0", label aster append word tex ctitle(GVA per hour \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin0", label aster append word tex ctitle(Labor share) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted4
    outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_tmin0", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
	

////////////////////// c)  with Hong Kong and Singapore ///////////////////////	

* Create Table B15, Panel C
	
use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing==1
	keep if robotsfirm==0
	merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare_KR_TW_SG_HK.dta", nogen keep(1 3)

	egen beidXyear = group(beid year)
	egen industry_ifrXyear = group(industry_ifr year)
	g sbi2 = substr(sbi,1,2)
	destring sbi2, force replace
	egen sectorXyear = group(sector year)

ivreghdfe logvalueadded (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted1
ivreghdfe laborshare (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted2
ivreghdfe loghwage (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted3
ivreghdfe loghoursworked (compete=robotsexposure_tmin5) robots [weight=$feweightvar], absorb($fecvar) cluster($cluster industry_ifrXyear)
	estimates store weighted4

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_KR-TW-SG-HK", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.) addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted2
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_KR-TW-SG-HK", label aster append word tex ctitle(GVA per hour \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_KR-TW-SG-HK", label aster append word tex ctitle(Labor share) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2
qui estimates restore weighted4
    outreg2 using "Robots and Workers\Results\Panelregressions_Competition_IV_KR-TW-SG-HK", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon addstat("Kleibergen-Paap \textit{F}-statistic", e(widstat)) nor2


